/* 
 * File:   HttpServerEventListener.cpp
 * Author: dell
 * 
 * Created on 2012年4月4日, 下午10:25
 */

#include "HttpServerEventListener.h"
#include "RapidServer.h"
#include "RapidHttpServlet.h"
//#include "MainProcessQuitHandler.h"

using namespace std;

namespace Rapid{
    HttpServerEventListener::HttpServerEventListener(RapidServer* rapidServer):
        rapidServer(rapidServer),mainProcessQuitHandler(rapidServer) {
        
    }

    HttpServerEventListener::~HttpServerEventListener() {
    }
    
    bool HttpServerEventListener::handle(const Event* e){
        switch(e->getType()){
            case EVENT_TYPE_PROCESS_STARTED:
                return onProcessStartedEvent((const ProcessStartedEvent*)e);
            case EVENT_TYPE_PROCESS_RECEIVE_MESSAGE:
                return onProcessReceiveMessageEvent((const ProcessReceiveMessageEvent*)e);
        }
        //LOG_DEBUG("e->getType():%d", e->getType());
        
        return false;
    }    
    
    
    bool HttpServerEventListener::onProcessReceiveMessageEvent(const ProcessReceiveMessageEvent* e){
        if(e->succeed && !e->childProcess && e->message){
            if(e->message->type == MSG_TYPE_CACHE_FILE){
                LOG_DEBUG("e->message->type:%d", e->message->type)
                return rapidServer->fileCached.storeFile((const char*)e->message->data);
            }
        }
        return false;
    }
    
    
    bool HttpServerEventListener::onProcessStartedEvent(const ProcessStartedEvent* e){
        
        if(rapidServer->isDaemon){
            //因为子进程会继承父进程的资源，所以在子进程启动后需要清理不需要的
            LOG_DEBUG("is daemon, inChildProcess:%d, allRapidServers.size:%d", e->inChildProcess, allRapidServers.size());
            //清除从父进程中带来的资源
            AllRapidServers::iterator it = allRapidServers.begin();
            for(; it != allRapidServers.end();it++){
                delete *it;
            }
            allRapidServers.clear();
            
        }else if(e->inChildProcess){//非daemon模式，在子进程中
            LOG_DEBUG("inChildProcess, is run, stop other RapidServer");
            //清除从父进程中带来的资源
            //停止从父进程中带过来的RapidServer
            AllRapidServers::iterator it = allRapidServers.begin();
            for(; it != allRapidServers.end();it++){
                (*it)->stop();
            }
        }
        
        //启动进程消息处理程序
        if(e->inChildProcess){
//            LOG_DEBUG("inChildProcess, is run, start childProcessQuitHandler");
            //停止从父进程中带过来的mainProcessQuitHandler
            mainProcessQuitHandler.stop(); 
            //在子进程中启动ChildProcessQuitHandler
            childProcessQuitHandler.start();
            
            rapidServer->fileCached.delAllSourceFileEvstats();
            
        }else{
            //在主进程中，启动MainProcessQuitHandler
//            LOG_DEBUG("mainProcessQuitHandler.isStarted():%d", mainProcessQuitHandler.isStarted());
            if(!mainProcessQuitHandler.isStarted()){
                mainProcessQuitHandler.start();
            }
        }
        
        
        return true;
    }    
    
}
